<?php $this->beginContent('//layouts/main'); ?>
	<? $this->widget('application.extensions.mcdropdown.McDropdown'); ?>
	<div class="section">
		
		<div class="box small">
		<? if ($this->modelCategory) : ?>
			<div class="title">
				<h2>Категории</h2>
				
			</div>
			
				
				
			
			<div class="content">
			
			<a href="#" onclick='category.createItem(); return false;' class="icon-add green" style="opacity: 1; "></a>
			<a href="#" onclick='category.loadItem(); return false;' class="icon-edit orange" style="opacity: 1; "></a>
			<a href="#" onclick='category.deleteItem(); return false;' class="icon-delete red" style="opacity: 1; "></a>
			
			<a href="#" class="icon-quest blue" style="opacity: 1; "></a>
				<? $module = Yii::app()->controller->module->id; ?>
				<script type="text/javascript" >
						$.fn.Category = function() {
										var obj = this;
							      this.defaultOptions = {
							          class: 'DataBar',
							          text: 'Enter Text Here',
							          formId: 'category-form',
							          categoryId: '<?= $_GET['cId']; ?>',
							          createUrl: '<?= Yii::app()->createUrl('/'.$module.'/category/create'); ?>',
							          updateUrl: '<?= Yii::app()->createUrl('/'.$module.'/category/update'); ?>',
							          loadUrl: '<?= Yii::app()->createUrl('/'.$module.'/category/load'); ?>',
							          deleteUrl: '<?= Yii::app()->createUrl('/'.$module.'/category/delete'); ?>',
							          returnUrl: '<?= Yii::app()->createUrl('/admin/'.$module.'?cId='); ?>'
							          
							      }

							      this.addItem = function() {
							          $(this).dialog("close");
										    $.ajax({
													type: "POST",
													url: obj.defaultOptions.createUrl,
													data: $('#'+obj.defaultOptions.formId).serialize()+'&ajax='+obj.defaultOptions.formId,
													success: function(ret) {
														var errors='';
														ret = eval('(' + ret + ')');
														if (ret['success']=='true') {
						 									window.location.href=obj.defaultOptions.returnUrl+ret['id'];
						 									return false;
														}					 
														jQuery.each(ret,function(i, item){
										                errors+='\n'+ item;
										                
										        });
										        alert(errors); 
														return false;
													}
											});
										};
										
										this.editItem = function() {
							          $(this).dialog("close");			        
								        $.ajax({
													type: "POST",
													url: obj.defaultOptions.updateUrl,
													data: $('#'+obj.defaultOptions.formId).serialize()+'&id='+obj.defaultOptions.categoryId+'&ajax='+obj.defaultOptions.formId,
													success: function(ret) {
														
														var errors='';
														ret = eval('(' + ret + ')');
														if (ret['success']=='true') {
						 									window.location.href=obj.defaultOptions.returnUrl+obj.defaultOptions.categoryId;
						 									return false;
														}					 
														jQuery.each(ret,function(i, item){
								                    errors+='\n'+ item;
								                    
								            });
								            alert(errors); 
														return false;
													}
											});
										};
										
										this.createItem = function() {
											$("#mydialog1").load(obj.defaultOptions.loadUrl, 'cId='+obj.defaultOptions.categoryId+'&ajax='+obj.defaultOptions.formId); 
											$("#mydialog2").dialog({
																title:'Добавить',
																buttons:{
																	'Сохранить': category.addItem, 
																	'Отмена': function(){ $(this).dialog("close");}
																}
															});
											$("#mydialog2").dialog("open"); 
											//$('#mydialog1').load(obj.defaultOptions.loadUrl);
											return false;
								       
										};
										
										this.loadItem = function() {
											$("#mydialog1").load(obj.defaultOptions.loadUrl, 'id='+obj.defaultOptions.categoryId+'&ajax='+obj.defaultOptions.formId); 
											$("#mydialog2").dialog({
																title:'Редактирование',
																buttons:{
																	'Сохранить': category.editItem, 
																	'Отмена': function(){ $(this).dialog("close");}
																}
															});
											$("#mydialog2").dialog("open"); 
											//$('#mydialog1').load(obj.defaultOptions.loadUrl);
											return false;
								       
										};
										
										this.deleteItem = function() {
				    						if(!confirm('Вы уверены, что хотите удалить данный элемент?')) return false;
								        $.ajax({
													type: "POST",
													url: obj.defaultOptions.deleteUrl,
													data: 'id='+obj.defaultOptions.categoryId+'&ajax='+obj.defaultOptions.formId,
													success: function(ret) {
														var errors='';
														ret = eval('(' + ret + ')');
														if (ret['success']=='true') {
						 									window.location.href=obj.defaultOptions.returnUrl;
						 									return false;
														}					 
														jQuery.each(ret,function(i, item){
								                    errors+='\n'+ item;
								                    
								            });
								            alert(errors); 
														return false;
													}
											});
										}
						} 

						var category = new $.fn.Category();
						
						$.fn.Cinema = function(){
									var obj = this;
							      this.defaultOptions = {
							          class: 'DataBar',
							          text: 'Enter Text Here',
							          formId: 'cinema-form',
							          cinemaId: '<?= $_GET['cId']; ?>',
							          getSelect: '<?= Yii::app()->createUrl('/'.$module.'/cinema/select'); ?>',
							          updateUrl: '<?= Yii::app()->createUrl('/'.$module.'/category/update'); ?>',
							          loadUrl: '<?= Yii::app()->createUrl('/'.$module.'/cinema/load'); ?>',
							          deleteUrl: '<?= Yii::app()->createUrl('/'.$module.'/category/delete'); ?>',
							          returnUrl: '<?= Yii::app()->createUrl('/admin/'.$module.'?cId='); ?>'   
							      };
							this.addDate = function(){
								$("#cinemadialog1").load(obj.defaultOptions.loadUrl, 'cId='+obj.defaultOptions.cinemaId+'&ajax='+obj.defaultOptions.formId); 
								$("#cinemadialog2").dialog({
																title:'Добавить',
																buttons:{
																	'Сохранить': cinema.addItem, 
																	'Отмена': function(){ $(this).dialog("close");}
																}
															});
								$("#cinemadialog2").dialog("open");
								return false; 
							};
							this.addItem = function() {
												data = $("#cinema-form2").serializeArray();
												id = $(data).get(0).value;
												$.ajax({
													type: "POST",
													url: obj.defaultOptions.getSelect,
													data: '&id='+id,
													success: function(ret) {
														cinemaName =  ret;
														var tr;
														tr += "<tr><td id=\"id\">";
														tr += id;
														tr += "</td>";
														tr += "<td id=\"cinema\">";
														tr += cinemaName;
														tr += "</td>";
														tr += "<td id=\"date\">";
														tr += $(data).get(2).value;
														tr += "</td>";
														tr +='<td><table id="times"><tr>';
														$.each(data,function(key,object){
															if(object.name=="time")
															{
															tr += "<td id=\"time\">";
															tr += object.value;
															tr += "</td>";
															}
														});
														tr +='</tr></table></td>';
														tr += "<td><button type=\"button\" class=\"blue medium button2\" onclick=\"cinema.addCinema(id)\"><span>[+]</span></button></td></tr></table>";
														$("#dates").append(tr); 
														return false;	
													}					  
											});
							          $(this).dialog("close");
							};
							this.writeCinema = function(){
								data = $("#event-form").serializeArray();
								var url = "";
								$("#dates > tbody >tr").each(function(){
									var $row = $(this);
									url += $row.find("#id").text() + "_";
									url += $row.find("#date").text() + "_";
									$row.find("#time").each(function(){
										url +=$(this).text()+",";
									});
									url += ";" 
								});
								console.log(url);
							};
						}
						var cinema = new $.fn.Cinema();
				</script>
				<?
				$this->widget('zii.widgets.CMenu',array(
				        'items'=>$this->category,
				        
				        'htmlOptions'=>array(
				                'class'=>'filetree treeview',//there are some classes that ready to use
                                             'id'=>'cmenu1'
				        ),
				));
				
				?>
					<? $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
					  'id'=>'mydialog2',
					  'options'=>array(
					      'title'=>'Добавить категорию',
					      'autoOpen'=>false,
					      'modal'=>true,
					      'width'=>349,
					      'height'=>259,
					      'buttons'=>array(
					          'Сохранить'=>'js:category.addItem',
					          'Отмена'=>'js:function(){ $(this).dialog("close");}',
					      ),
					  ),
				));
				?>
				<div id="mydialog1"></div>
				<? $this->endWidget('zii.widgets.jui.CJuiDialog');	 ?>			
			</div>
				
			<? endif; ?>
		</div>
		
		<div class="box big">			
			<?php echo $content; ?>
		</div>
	</div>
	
	<!-- Modal window for addCinema -->
	<? $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
					  'id'=>'cinemadialog2',
					  'options'=>array(
					      'title'=>'Добавить Кинотеатр',
					      'autoOpen'=>false,
					      'modal'=>true,
					      'width'=>500,
					      'height'=>400,
					      'buttons'=>array(
					          'Сохранить'=>'js:category.addItem',
					          'Отмена'=>'js:function(){ $(this).dialog("close");}',
					      ),
					  ),
				));
				?>
				<div id="cinemadialog1"></div>
				<? $this->endWidget('zii.widgets.jui.CJuiDialog');	 ?>
			</div>			
	<!-- End Modal window -->

<?php $this->endContent(); ?>

